package org.example.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.example.task.mapper.PermissionMapper;
import org.example.task.mapper.RolePermissionMapper;
import org.example.task.mapper.UserMapper;
import org.example.task.mapper.UserRoleMapper;
import org.example.task.model.po.*;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;
    private final RolePermissionMapper rolePermissionMapper;
    private final PermissionMapper permissionMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户名或者密码错误");
        }
        //查询用户权限封装到LoginUser中
        // 1.根据用户id查询角色id
        LambdaQueryWrapper<UserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(UserRole::getUserId,user.getUserId());
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleWrapper);
        // 2.根据角色id查询权限id
        ArrayList<Long> roleIds = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        if (!roleIds.isEmpty()) {
            LambdaQueryWrapper<RolePermission> rolePermissionWrapper = new LambdaQueryWrapper<>();
            rolePermissionWrapper.in(RolePermission::getRoleId,roleIds);
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionWrapper);
            // 3.权限id去重
            ArrayList<Long> permissionIds = new ArrayList<>();
            for (RolePermission rolePermission : rolePermissions) {
                permissionIds.add(rolePermission.getPermissionId());
            }
            ArrayList<Long> distinctPermissionIds = permissionIds.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
            // 4.根据权限id查询权限
            LambdaQueryWrapper<Permission> permissionWrapper = new LambdaQueryWrapper<>();
            permissionWrapper.in(Permission::getPermissionId,distinctPermissionIds);
            List<Permission> permissions = permissionMapper.selectList(permissionWrapper);
            List<String> list = new ArrayList<>();
            for (Permission permission : permissions) {
                list.add(permission.getPermissionName());
            }
            //封装成userDetails对象返回
            return new LoginUser(user,list,null);
        }
        return new LoginUser(user,null,null);
    }
}
